# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=mdbook-pikchr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.1.6
pkgrel=1
pkgdesc="A mdbook preprocessor to render pikchr code blocks as images in your book (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/podsvirov/mdbook-pikchr"
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-mdbook")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz::${url}/archive/v${pkgver}.tar.gz")
sha256sums=('d78aab2d34274153446b0d6ba6afa21e21a18ee14c7b226b87b5bf43669697b0')

prepare() {
  cd "${srcdir}"
  rm -rf "build-${MSYSTEM}" | true
  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  sed -e "s/https:\/\/raw.githubusercontent.com\/podsvirov\/mdbook-pikchr\/master\///" -i README.md
  sed -e "s/debug/release/" -i book.toml

  "${MINGW_PREFIX}/bin/cargo.exe" fetch \
    --locked
}

build() {
  msg "Cargo build for ${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}/bin/cargo.exe" build \
    --release \
    --locked

  msg "Build documentation"

  "${MINGW_PREFIX}/bin/mdbook.exe" build
  cd book
  rm -rf target src
  rm -f book.toml Cargo.lock Cargo.toml \
    build.rs LICENSE Makefile \
    .gitignore .nojekyll
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}/bin/cargo.exe" test \
    --release \
    --locked
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  install -Dm755 "target/release/${_realname}.exe" "${pkgdir}${MINGW_PREFIX}/bin/${_realname}.exe"
  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cp -a "book" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html"
  install -Dm644 "LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
